
import { RedTipPos } from "./RedTipConst";

let TAG = "RedTipPoint";

// let redSprite = "images/ui/pic/ui_icon_hongdian";

export default class BaseRedTipManager {

    private spriteFrame: cc.SpriteFrame;

    constructor(sf: cc.SpriteFrame) {
        this.spriteFrame = sf;
    }


    addSprite(widget: cc.Node,name: string): cc.Node {
        let child = widget.getChildByName(name);
        if (!child) {

            child = new cc.Node(name);
            let sprite = child.addComponent(cc.Sprite);
            sprite.spriteFrame = this.spriteFrame;

            widget.addChild(child);
        }else{
            child.opacity = 255;
            cc.warn('BaseRedTipManager addSprite  name is have  ',name)
        }
        return child;
    }

    addRedSprite(widget: cc.Node, data) {
        let posData = data[RedTipPos.position];
        let pos = this.getPos(widget);
        if (!posData) {
            pos = cc.v2(pos.x - 5, pos.y - 5);
        } else {
            pos = cc.v2(pos.x + posData.x, pos.y + posData.y);
        }
        let child = this.addSprite(widget, TAG);
        if (child) {
            child.setPosition(pos);
        }
    }


    deleteRedSprite(widget: cc.Node) {
        let child: cc.Node = widget.getChildByName(TAG);
        if (child) {
            child.opacity = 0
            // widget.removeChild(child);
        }
    }

    getPos(widget:cc.Node,a:cc.Vec2 = cc.v2(1,1)):cc.Vec2{
        let anchor = widget.getAnchorPoint();  
        let disx = a.x - anchor.x ;
        let disy = a.y - anchor.y; 
        let x = disx * widget.width;
        let y = disy * widget.height;
        return cc.v2(x,y)
    }
}
